package com.lagou.edu.utils;

import com.lagou.edu.anno.Component;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * 获取当前线程的数据库链接
 * @author wsz
 * @date 2021/10/18 19:39
 **/
@Component
public class ConnectionUtils {

    private ThreadLocal<Connection> connectionThreadLocal = new ThreadLocal<>();

    public Connection getCurrentConnection() throws SQLException {
        Connection connection = connectionThreadLocal.get();
        if (connection == null) {
            connection = DruidUtils.getInstance().getConnection();
            connectionThreadLocal.set(connection);
        }
        return connection;
    }
}
